package cn.piaoyi.web.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cn.piaoyi.bean.Admin;
import cn.piaoyi.bean.UserInfo;
import cn.piaoyi.comm.Constant;
import cn.piaoyi.comm.Util;
import cn.piaoyi.exception.PiaoyiException;
import cn.piaoyi.service.AdminService;
import cn.piaoyi.service.UserInfoService;
import cn.piaoyi.service.web.PageList;
import cn.piaoyi.service.web.Pages;
import cn.piaoyi.web.servlet.AdminSession;
import cn.piaoyi.web.ui.OptionsInt;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.SessionAware;

public class AdminUserInfo extends BaseAction{
	private static final Log logger = LogFactory.getLog(AdminUserInfo.class);
	
	private UserInfoService userInfoService;
	
	public UserInfoService getUserInfoService() {
		return userInfoService;
	}

	public void setUserInfoService(UserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}
	
	private List validatedList = new ArrayList();

	public List getValidatedList() {
		return validatedList;
	}

	public void setValidatedList(List validatedList) {
		this.validatedList = validatedList;
	}
	
	private void setValidatedListValues(){
		validatedList.add(new OptionsInt(-1, "请选择"));
		validatedList.add(new OptionsInt(0, "未激活"));
		validatedList.add(new OptionsInt(1, "激活"));
		validatedList.add(new OptionsInt(2, "禁用"));
	}
	
	private String account = "";	//账号
	private int validated = -1;		//认证状态
	private long id;				//ID

	public String getAccount() {
		return account;
	}

	public void setAccount(String account) {
		this.account = account;
	}

	public int getValidated() {
		return validated;
	}

	public void setValidated(int validated) {
		this.validated = validated;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}
	
	
	private PageList pageList;
	private UserInfo userInfo;
	
	public PageList getPageList() {
		return pageList;
	}

	public void setPageList(PageList pageList) {
		this.pageList = pageList;
	}

	public UserInfo getUserInfo() {
		return userInfo;
	}

	public void setUserInfo(UserInfo userInfo) {
		this.userInfo = userInfo;
	}

	public String execute() {
		try {
			return this.executeMethod(this.getAction());
		} catch (Exception e) {
			logger.error(e);
			return INPUT;
		}
	}

	/**
	 * 初始页
	 */
	public String index(){
		setValidatedListValues();
		Pages pages = new Pages();
		pages.setPage(this.getPage());
		pages.setPerPageNum(Constant.PAGE_NUM);
		if (this.getTotal() > 0) {
			pages.setTotalNum(this.getTotal());
		}
		pages.setFileName("/admin_userinfo.action?account="+this.account+"&validated="+this.validated);
		this.setPageList(this.getUserInfoService().findUserInfos(this.account,this.validated,"id", 1, pages));
		return SUCCESS;
	}

	public String show(){
		this.setUserInfo(this.getUserInfoService().findUserInfoById(this.id));
		return "show";
	}

	public String validated(){
		this.userInfo = this.getUserInfoService().findUserInfoById(this.id);
		this.userInfo.setValidated(this.validated);
		try {
			this.getUserInfoService().updateUserInfo(this.userInfo);
		} catch (PiaoyiException e) {
			this.addActionError(this.getText("error.saveerror"));
			return ERROR;
		}
		return "doshow";
		
	}

}
